package com.zzj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzj.base.Result;
import com.zzj.entity.Feedback;
import com.zzj.model.dto.QueryListDTO;
import com.zzj.model.dto.feedback.FeedbackCreateDTO;
import com.zzj.model.dto.feedback.FeedbackReplyDTO;
import com.zzj.service.IFeedbackService;
import com.zzj.utils.QueryUtil;
import com.zzj.utils.Utils;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-04-10
 */
@RestController
@RequestMapping("/feedback")
public class FeedbackController {

    @Resource
    private IFeedbackService feedbackService;




    @PostMapping("list")
    public Result list (@RequestBody @Validated QueryListDTO in){
        QueryWrapper<Feedback> queryWrapper = QueryUtil.paramsWrapper(in.getAndParams(), in.getOrParams(), in.getOrderParams());
        Page<Feedback> feedbackPage = new Page<>(in.getPageNo(), in.getPageSize());
        IPage<Feedback> feedbackIPage = feedbackService.getBaseMapper().selectPage(feedbackPage, queryWrapper);
        return Result.success(feedbackIPage);
    }

    @PostMapping("create")
    public Result create(@RequestBody @Validated FeedbackCreateDTO in){
        Feedback feedback = JSONObject.parseObject(JSON.toJSONString(in), Feedback.class);
       feedbackService.save(feedback);
        return Result.success("反馈成功");
    }
    @PostMapping("reply")
    public Result reply (@RequestBody @Validated FeedbackReplyDTO in){

        feedbackService.lambdaUpdate()
                .eq(Feedback::getId, in.getId())
                .set(Feedback::getReplyContent, in.getReplyContent())
                .set(Feedback::getReplyTime, Utils.getNowString())
                .update();
        return Result.success("回复成功");
    }



}
